#include "stdafx.h"

#include <MyFC/ColorBox.h>
#include <MyFC/Utility.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

namespace My {

ColorBox::ColorBox()
{
	m_Size = CSize(11, 11);
	m_Alignment = ALIGN_VCENTER|ALIGN_HLEFT;
}			 

CSize ColorBox::GetPreferredSize() const
{
	return CSize(m_Size.cx + 2, m_Size.cy + 2);
}

void ColorBox::SetAlignment(int a)
{
	if (a != m_Alignment) {
		m_Alignment = a;
		Invalidate();
	}
}

void ColorBox::HandlePaint(CDC * pDC, const CRect & r)
{
	SaveObjects save(pDC);
	
	CRect rClient = ClientRect();
	//rClient.DeflateRect(4, 2);

	CRect rButton(CPoint(0, 0), m_Size);
	rButton.InflateRect(1, 1);
	rButton = AlignRect(rClient, rButton, m_Alignment);

	if (rButton.Width() > rClient.Width() ||
		rButton.Height() > rClient.Height())
		return;

	COLORREF cL = GetSysColor(COLOR_BTNHILIGHT);
	COLORREF cS = GetSysColor(COLOR_BTNSHADOW);
	pDC->Draw3dRect(&rButton, cS, cL);

	if (ControlEnabled(this)) {

		COLORREF cBlack = RGB(0, 0, 0);
		COLORREF cWhite = RGB(255, 255, 255);
		rButton.InflateRect(-1, -1);
		pDC->Draw3dRect(&rButton, cBlack, cBlack);
		rButton.InflateRect(-1, -1);
		pDC->Draw3dRect(&rButton, cWhite, cWhite);
		rButton.InflateRect(-1, -1);

		COLORREF c;
		if (GetColor(c)) {
			CBrush bFace(c);	
			pDC->FillRect(&rButton, &bFace);
		}
	}
}

void ColorBox::HandleClick()
{
	if (ControlEnabled(this)) {
		COLORREF c = RGB(0, 0, 0);
		GetColor(c);
		CColorDialog dlg(c, CC_ANYCOLOR|CC_FULLOPEN, GetWindow());
		if (dlg.DoModal() == IDOK)
			SetColor(dlg.GetColor());
	}
}

} // My

